<?php
/*
 * Created on 21 mars 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */

class UserDAO extends DAO{


	function getById($id){
		try
		{
			$id = Validator::escapeSql($id);
			$query="select * from USER where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = new User((int)$Bean->ID);
				$thebean->setName($Bean->NAME);
				$thebean->setSurname($Bean->SURNAME);
				//$thebean->setPseudo($Bean->PSEUDO);
				$thebean->setEmail($Bean->EMAIL);
				$thebean->setStreet($Bean->STREET);
				$thebean->setNumber($Bean->NUMBER);
				$thebean->setZip($Bean->ZIP);
				$thebean->setCity($Bean->CITY);
				$thebean->setCountry($Bean->COUNTRY);
				$thebean->setLangue($this->factory->getLangueDAO()->getById((int)$Bean->LANGUE_ID));
				if(!is_null($Bean->LAST_IP)){
					$thebean->setIp($Bean->LAST_IP);
				}

				list($year,$month,$day)=split('[/,-]',$Bean->DDN);
				$thebean->setDdn($day,$month,$year);
				$thebean->setCreated($Bean->CREATED);
				$thebean->setLastConnected($Bean->LAST_CONNECTED);
				$thebean->setPwsHash($Bean->PWS_HASH,$Bean->PWS_HASH);

				$thebean->setProfile($this->factory->getProfileDAO()->getById((int)$Bean->PROFILE_ID));
				$thebean->setActive($Bean->ACTIVE);
			}
		}
		catch(Exception $e)     {
			throw $e;
			 
		}

		return $thebean;
	}

	function add($bean){
		$thebean = null;
		if (is_null($bean->getId())||$bean->getId()==0){
			$stamp = time();
			$query="insert into USER (name,surname,pseudo,email,street,number,zip,city,country,langue_id,last_ip,ddn,pws_hash,profile_id,created,last_connected)";
			$query.=" values ('".Validator::escapeSql($bean->getName())."','".Validator::escapeSql($bean->getSurname())."','".Validator::escapeSql($bean->getPseudo())."','".Validator::escapeSql($bean->getEmail())."','".Validator::escapeSql($bean->getStreet())."','".Validator::escapeSql($bean->getNumber())."','".Validator::escapeSql($bean->getZip())."','".Validator::escapeSql($bean->getCity())."','".Validator::escapeSql($bean->getCountry())."',".Validator::escapeSql($bean->getLangue()->getId()).",'".Validator::escapeSql($bean->getIp())."',DATE('".Validator::escapeSql($bean->getDdn())."'),'".Validator::escapeSql($bean->getPwsHash())."',".Validator::escapeSql($bean->getProfile()->getId()).",NOW(),NOW());";
			try{
				$this->pdo->exec($query);
				$thebean = $this->getById((int)$this->pdo->lastInsertId());
			}catch(Exception $e){
				 
				echo $e->getMessage();
			}
		}
		return $thebean;
	}
	function getByEmail($email){

		try
		{
			$email = Validator::escapeSql($email);
			$query="select * from USER where email='$email';";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
			}
		}
		catch(Exception $e)     {
			echo "<br/>QUERY:".$query."<br/>";
			throw $e;
		}

		return $thebean;
	}

	function getAnonymousUser(){
		 
		try
		{
			$query="select * from USER where id=(select id from USER where profile_id=(select id from PROFILE where lower(label)='anonymous'));";
			 
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$thebean = null;
			foreach($array_obj as $Bean){
				$thebean = $this->getById((int)$Bean->ID);
			}
		}
		catch(Exception $e)     {
			throw $e;
			echo $query;
		}

		return $thebean;
	}
	public function update($Bean){
		try{
			$query = "update USER set name='".Validator::escapeSql($Bean->getName())."'";
			$query.=",surname='".Validator::escapeSql($Bean->getSurname())."'";
			$query.=",pseudo ='".Validator::escapeSql($Bean->getPseudo())."'";
			$query.=",email ='".Validator::escapeSql($Bean->getEmail())."'";
			$query.=",street ='".Validator::escapeSql($Bean->getStreet())."'";
			$query.=",number ='".Validator::escapeSql($Bean->getNumber())."'";
			$query.=",zip ='".Validator::escapeSql($Bean->getZip())."'";
			$query.=",city ='".Validator::escapeSql($Bean->getCity())."'";
			$query.=",country ='".Validator::escapeSql($Bean->getCountry())."'";
			$query.=",langue_id =".Validator::escapeSql($Bean->getLangue()->getId());
			if(!is_null($Bean->getIp())){
				$query.=",last_ip ='".Validator::escapeSql($Bean->getIp())."'";
			}
			$query.=",last_connected = CURRENT_TIMESTAMP";
			$query.=",pws_hash ='".Validator::escapeSql($Bean->getPwsHash())."'";
			$query.=",profile_id =".Validator::escapeSql($Bean->getProfile()->getId());
			$query.=",active =".Validator::escapeSql($Bean->isActive());
			$query.=",ddn =DATE('".Validator::escapeSql($Bean->getDdn())."')";
			$query.=" where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;
			echo "<br/>QUERY:".$query."<br/>";
			echo $e->getMessage();
		}
		return $bool;
	}

	public function remove($Bean){
		try{
			$queryFK="update USER set active=false where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;
	}
}
?>